#!/bin/bash
# ClamAV病毒库定时任务安装脚本

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CRON_FILE="$SCRIPT_DIR/clamav_cron.conf"
PYTHON_SCRIPT="$SCRIPT_DIR/clamav_updater.py"

# 检查文件是否存在
if [ ! -f "$CRON_FILE" ]; then
    echo "错误：找不到cron配置文件 $CRON_FILE"
    exit 1
fi

if [ ! -f "$PYTHON_SCRIPT" ]; then
    echo "错误：找不到Python脚本 $PYTHON_SCRIPT"
    exit 1
fi

# 确保Python脚本有执行权限
chmod +x "$PYTHON_SCRIPT"

# 备份现有的crontab（如果存在）
if crontab -l > /dev/null 2>&1; then
    echo "备份现有crontab..."
    crontab -l > /tmp/crontab_backup_$(date +%Y%m%d_%H%M%S)
fi

# 添加新的cron任务
echo "安装ClamAV病毒库定时任务..."
cat "$CRON_FILE" | crontab -

# 验证安装
echo "验证cron任务安装..."
crontab -l

echo ""
echo "安装完成！"
echo "定时任务将在每周二凌晨3点自动执行"
echo ""
echo "手动执行命令："
echo "  $PYTHON_SCRIPT"
echo ""
echo "查看cron任务："
echo "  crontab -l"
echo ""
echo "删除cron任务："
echo "  crontab -l | grep -v clamav_updater | crontab -"
echo ""
echo "查看日志："
echo "  tail -f /var/log/clamav_updater.log"